package org.nicocube.airain.admin.server;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.nicocube.airain.admin.client.AdminService;
import org.nicocube.airain.domain.client.exception.DomainException;
import org.nicocube.airain.domain.client.gamedate.GameDate;
import org.nicocube.airain.domain.client.gamedate.GameDateManager;
import org.nicocube.airain.domain.server.dao.DomainDAO;
import org.nicocube.airain.domain.server.storable.GameDateManagerDAO;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class AdminServiceImpl extends RemoteServiceServlet implements
		AdminService {

	private static final Logger log = Logger.getLogger(AdminServiceImpl.class.getName());
	
	private static final long serialVersionUID = 1L;

	private GameDateManagerDAO gameDateManagerDAO;

	public AdminServiceImpl() throws DomainException {
		gameDateManagerDAO = DomainDAO.getDao(GameDateManagerDAO.class);
	}
	
	@Override
	public Map<String,GameDate> retrieveX() throws DomainException {
		GameDateManager gameDateManager = retrieve();
		Map<String, GameDate> res = new HashMap<String, GameDate>();
		res.put("beginning",gameDateManager.getBeginning());
		res.put("current",gameDateManager.getCurrent());
		res.put("last",gameDateManager.getLast());
		return res;
	}
	
	public GameDateManager retrieve() throws DomainException {
		try {
			return gameDateManagerDAO.getSingleton();
		} catch (DomainException e) {
			log.log(Level.SEVERE, "Service failed", e);
			throw e;
		}
	}

}
